/**
 * 
 */
package freedom.ms.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.security.oauth2.provider.token.store.redis.BaseRedisTokenStoreSerializationStrategy;
import org.springframework.stereotype.Component;

/**
 * @author charlie
 *
 */
@Component
public class GenericJackson2JsonSerializationStrategy extends BaseRedisTokenStoreSerializationStrategy {
	
    @Autowired
    private GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer;
    @Autowired
    private StringRedisSerializer stringRedisSerializer;

	/* (non-Javadoc)
	 * @see org.springframework.security.oauth2.provider.token.store.redis.BaseRedisTokenStoreSerializationStrategy#deserializeInternal(byte[], java.lang.Class)
	 */
	@Override
	protected <T> T deserializeInternal(byte[] bytes, Class<T> clazz) {
		return genericJackson2JsonRedisSerializer.deserialize(bytes, clazz);
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.oauth2.provider.token.store.redis.BaseRedisTokenStoreSerializationStrategy#deserializeStringInternal(byte[])
	 */
	@Override
	protected String deserializeStringInternal(byte[] bytes) {
		return stringRedisSerializer.deserialize(bytes);
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.oauth2.provider.token.store.redis.BaseRedisTokenStoreSerializationStrategy#serializeInternal(java.lang.Object)
	 */
	@Override
	protected byte[] serializeInternal(Object object) {
		return genericJackson2JsonRedisSerializer.serialize(object);
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.oauth2.provider.token.store.redis.BaseRedisTokenStoreSerializationStrategy#serializeInternal(java.lang.String)
	 */
	@Override
	protected byte[] serializeInternal(String data) {
		return stringRedisSerializer.serialize(data);
	}

}
